package com.example.administrator.newxlmedical;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import adapters.UserViewPagerAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fragments.UserManageFragment;
import fragments.UserRegisterFragment;
import fragments.UserRightsFragment;
import utils.ViewUtil;
import view.ControlScrollViewPager;


public class UserActivity extends BaseActivity {
    @BindView(R.id.tl_user_activity)
    TabLayout tabLayout;
    @BindView(R.id.vp_user_activity)
    public ControlScrollViewPager viewPager;
    @BindView(R.id.iv_common_toolbar_home)
    ImageView ivHome;
    private UserViewPagerAdapter pagerAdapter;
    private List<Fragment> fragmentList = new ArrayList<>();
    private List<String> titles = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
        super.initView();
        UserRightsFragment userRightsFragment = new UserRightsFragment();
        UserManageFragment userManageFragment = new UserManageFragment();
        UserRegisterFragment userRegisterFragment = new UserRegisterFragment();

        fragmentList.add(userRightsFragment);
        fragmentList.add(userManageFragment);
        fragmentList.add(userRegisterFragment);
        titles.add("用户权限");
        titles.add("用户管理");
        titles.add("用户注册");

        pagerAdapter = new UserViewPagerAdapter(getSupportFragmentManager(), fragmentList, titles, this);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.getTabAt(0).setCustomView(pagerAdapter.getTabView(0));
        tabLayout.getTabAt(1).setCustomView(pagerAdapter.getTabView(1));
        tabLayout.getTabAt(2).setCustomView(pagerAdapter.getTabView(2));

        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                ViewUtil.setIndicator(tabLayout, 50, 50);
            }
        });
    }
    @Override
    protected void setListener() {
        super.setListener();
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                ImageView imageView = (ImageView) customView.findViewById(R.id.iv_tablayout_custom);
                TextView textView = (TextView) customView.findViewById(R.id.tv_tablayout_custom);
                int position = tab.getPosition();
                if (position == 0) {
                    imageView.setImageResource(R.mipmap.user_rights_blue);
                } else if (position == 1) {
                    imageView.setImageResource(R.mipmap.user_manage_blue);
                } else {
                    imageView.setImageResource(R.mipmap.user_register_blue);
                }
                textView.setTextColor(getResources().getColor(R.color.date_bg_color));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                ImageView imageView = (ImageView) customView.findViewById(R.id.iv_tablayout_custom);
                TextView textView = (TextView) customView.findViewById(R.id.tv_tablayout_custom);
                int position = tab.getPosition();
                if (position == 0) {
                    imageView.setImageResource(R.mipmap.user_rights_gray);
                } else if (position == 1) {
                    imageView.setImageResource(R.mipmap.user_manage_gray);
                } else {
                    imageView.setImageResource(R.mipmap.user_register_gray);
                }
                textView.setTextColor(getResources().getColor(R.color.tablayout_text_unselect));

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @OnClick(R.id.iv_common_toolbar_home)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_common_toolbar_home:
                finish();
                break;
        }
    }


}
